﻿@model OnlineViewModel
@using VideoSeller.Business.Data.Enums

<script type="text/javascript">
    function number_format(_number, _cfg) {
        function obj_merge(obj_first, obj_second) {
            var obj_return = {};
            for (key in obj_first) {
                if (typeof obj_second[key] !== 'undefined') obj_return[key] = obj_second[key];
                else obj_return[key] = obj_first[key];
            }
            return obj_return;
        }
        function thousands_sep(_num, _sep) {
            if (_num.length <= 3) return _num;
            var _count = _num.length;
            var _num_parser = '';
            var _count_digits = 0;
            for (var _p = (_count - 1) ; _p >= 0; _p--) {
                var _num_digit = _num.substr(_p, 1);
                if (_count_digits % 3 == 0 && _count_digits != 0 && !isNaN(parseFloat(_num_digit))) _num_parser = _sep + _num_parser;
                _num_parser = _num_digit + _num_parser;
                _count_digits++;
            }
            return _num_parser;
        }
        if (typeof _number !== 'number') {
            _number = parseFloat(_number);
            if (isNaN(_number)) return false;
        }
        var _cfg_default = { before: '', after: '', decimals: 2, dec_point: '.', thousands_sep: ',' };
        if (_cfg && typeof _cfg === 'object') {
            _cfg = obj_merge(_cfg_default, _cfg);
        }
        else _cfg = _cfg_default;
        _number = _number.toFixed(_cfg.decimals);
        if (_number.indexOf('.') != -1) {
            var _number_arr = _number.split('.');
            var _number = thousands_sep(_number_arr[0], _cfg.thousands_sep) + _cfg.dec_point + _number_arr[1];
        }
        else var _number = thousands_sep(_number, _cfg.thousands_sep);
        return _cfg.before + _number + _cfg.after;
    }

    function saveStepData() {
        window.OnlineContent.QualityString = $('#QualityStringsSelect').val();
        window.OnlineContent.QualityStringCost = camparcostr[$('#QualityStringsSelect').val()];
        //
        window.OnlineContent.ContentTypeMult = contenttypecost[$('#ddlContentType').val()];
        window.OnlineContent.VideoContentType = $('#ddlContentType').val();
        //
        window.OnlineContent.ViewrCnt = $('#VieversCountSelect').val();
        window.OnlineContent.ViewrCntCost = viewrcntcost[$('#VieversCountSelect').val()];
        //
        window.OnlineContent.MaxTimeMult = contenttymecost[$('#MaxTimeMult').val()];
        window.OnlineContent.MaxTime = $('#MaxTimeMult').val();
        if ($("#freePriceCheckbox")[0].checked) {
            
            window.OnlineContent.Price = $("#PriceFree").val();
            
        } else { window.OnlineContent.Price = '0' }

    }

    function loadStepData() {

        $("#wizard form").validate({
            rules: {
                QualityStringsSelect: { required: true }
            }
        });

        $("#QualityStringsSelect").change(function () {
            computeMany();
        });
        $("#ddlContentType").change(function () {

            computeMany();
            var d = document.getElementById('PriceFree');
            if (d)
            {
               // alert(checkMany);
                checkMany(d, '@Model.MaxPriceMyVSM', '@Model.MaxPriceConferenceVSM', 'MaxPriceMyVSMOverflow', 'MaxPriceConferenceVSMOverflow');
            }
        });
        $("#VieversCountSelect").change(function () {
            computeMany();
        });
        $("#MaxTimeMult").change(function () {
            computeMany();
        });
        
        if (window.OnlineContent.MaxTime) $("#MaxTimeMult").val(window.OnlineContent.MaxTime);
        if (window.OnlineContent.VideoContentType) $('#ddlContentType').val(window.OnlineContent.VideoContentType);
        if (window.OnlineContent.QualityString) $('#QualityStringsSelect').val(window.OnlineContent.QualityString);
        if (window.OnlineContent.ViewrCnt) $('#VieversCountSelect').val(window.OnlineContent.ViewrCnt);
        //if (window.OnlineContent.Price != "") {
        var op = window.OnlineContent.Price;
        if (op == "")
            op = "1";
                $("#PriceFree").val(number_format(op, { decimals: 2, thousands_sep: "" }));
                $("#PriceFree").show();
                $("#lblPriceFree").show();
                if (parseFloat(op) > 0)
                    $("#freePriceCheckbox").attr("checked", "checked");
                else
                    $("#freePriceCheckbox")[0].checked = false;
        //}
        
        
        computeMany();
        setNotFree($("#freePriceCheckbox").is(":checked"));
    }

    @{<text>var userMany = parseFloat(@Model.manyUser.ToString().Replace(",", "."))</text>}
    function computeMany() {
        var x = 0;
        if (!$("#freePriceCheckbox").is(":checked")) {
            x = parseFloat(@Model.BasicPublishPrice) *
                parseFloat(camparcostr[$('#QualityStringsSelect').val()])
                * parseFloat(contenttypecost[$('#ddlContentType').val()])
                * parseFloat(viewrcntcost[$('#VieversCountSelect').val()])
                * parseFloat(contenttymecost[$('#MaxTimeMult').val()])
        }
        var a = "";

        if (x <= 0) {
            a = '@Html.GetGlobalResource("Flash", "FreePublishing")';
            $('#NeedManyText').hide();
            $('#SmallManyTextW').hide();

        }
        else {
            a = x;
            if (x > userMany)
                $('#SmallManyTextW').show();
            else
                $('#SmallManyTextW').hide();
            $('#NeedManyText').show();
        }
        //var d = document.getElementById("NeedManyValue");
        $('#NeedManyValue').text(a);
        if (x <= userMany)
            return true;
        else
            return false;
    }
    function ifAllOk()
    {
        var allOk = computeMany();
        if (allOk)
        {
            allOk = ($("#MaxPriceConferenceVSMOverflow").is(":hidden") && $("#MaxPriceMyVSMOverflow").is(":hidden"));
            if(allOk && $("#freePriceCheckbox")[0].checked)
            {
                var d = document.getElementById('PriceFree');

                allOk = !isNaN(parseFloat(d.value)) && isFinite(d.value)
                && parseFloat(d.value) > 0;
                if (!allOk)
                {
                    
                    if (!(parseFloat(d.value) > 0))
                        $('#PriceRequired').show();
                    else
                        $('#NumbersAllowed').show();
                        
                }
            }
                
        }
        return allOk;
    }
    function checkMany(v, maxmanyfree, maxmanyconf, errDivFree, errDivConf) {
        //  alert(v.value);
        var maxmany;
        var errDiv;
        var odiv;
        $('#PriceRequired').hide();
        $('#NumbersAllowed').hide();

        if ($('#ddlContentType').val() == "@VideoContentType.Free.ToString()") {
            maxmany = maxmanyfree;
            errDiv = errDivFree;
            odiv = errDivConf;
        }
        else {
            maxmany = maxmanyconf;
            errDiv = errDivConf;
            odiv = errDivFree;
        }

        var ediv = document.getElementById(errDiv);
        var odiv = document.getElementById(odiv);
        if (odiv)
            odiv.style.display = "none";
      //  alert(errDivFree + "/" + errDivConf);
        if (v.value && $("#freePriceCheckbox").is(":checked")) {
            //      alert("");
           // v.value = v.value.replace("")
            if (v.value - maxmany > 0) {
                if (ediv)
                    ediv.style.display = "block";
                return;
            }
        }
        if (ediv)
            ediv.style.display = "none";
    }
    var camparcostr = [];
    @foreach (var item in Model.QualityStringCosts)
                    {
                   <text>camparcostr["@item.Key"] = '@item.Value.ToString().Replace(",", ".")'; </text>
                    }
    var viewrcntcost = [];
    @foreach (var item in Model.MaxViewerCntMult)
                    {
                   <text>viewrcntcost["@item.Key"] = '@item.Value.ToString().Replace(",", ".")'; </text>
                    }
    var contenttypecost = [];
    @foreach (var item in Model.ContentTypeMult)
                    {
                   <text>contenttypecost["@item.Key"] = '@item.Value.ToString().Replace(",", ".")'; </text>
                    }
    var contenttymecost = [];
    @foreach (var item in Model.MaxTimeMult)
                    {
                   <text>contenttymecost["@item.Key"] = '@item.Value.ToString().Replace(",", ".")'; </text>
                    }
    $("#freePriceCheckbox").change(function () {
        setNotFree($("#freePriceCheckbox")[0].checked);
    });
    function setNotFree(nfr) {
        var d = document.getElementById('PriceFree');
        if (nfr) {
            $("#PriceFree").show();
            $("#freeBlock").hide();
            $("#lblPriceFree").show();
           
            if (d) {
                checkMany(d, '@Model.MaxPriceMyVSM', '@Model.MaxPriceConferenceVSM', 'MaxPriceMyVSMOverflow', 'MaxPriceConferenceVSMOverflow');
            }
            $("#NeedManyValue").hide();
            
        } else {
            $("#PriceFree").hide();
            $("#freeBlock").show();
            $("#lblPriceFree").hide();
            $("#MaxPriceMyVSMOverflow").hide();
            if (d) {
                checkMany(d, '@Model.MaxPriceMyVSM', '@Model.MaxPriceConferenceVSM', 'MaxPriceMyVSMOverflow', 'MaxPriceConferenceVSMOverflow');
            }
            $("#NeedManyValue").show();

        }
        computeMany();
    }

</script>
    
<form>
    <div class="steps">
        <h4>Step 1 of 5</h4>
    </div>
    <div class="wizardtable">
        <table class="wz_s0_mainpar_t">
            <tr>
                <td>
                    <label>@Html.GetGlobalResource("OnlineWizard", "VideoContentType")</label>
                    <select id="ddlContentType" name="ddlContentType">
                        @foreach (var item in Model.ContentTypeMult)
                        {
                            <option value="@item.Key">@Html.GetGlobalResource("OnlineWizard", "VideoContentType" + @item.Key)</option>
                        }
                    </select>
                </td>
                <td style="width:50%;">
                    <label>@Html.GetGlobalResource("OnlineWizard", "QualityStringsSelect")</label>
                    <select id="QualityStringsSelect" name="QualityStringsSelect">
                        @foreach (var item in Model.QualityStringCosts)
                        {
                            <option value="@item.Key">@Html.GetGlobalResource("Flash", @item.Key)</option>
                        }
                    </select>
                </td>
            </tr>
        </table>

        <div class="wz_s0_price_b">
            <input id="freePriceCheckbox" type="checkbox" />&nbsp;<label style="display:inline;" for="freePriceCheckbox">@Html.GetGlobalResource("OnlineWizard", "PriceFree")</label>&nbsp;
            <input type="text" id="PriceFree" onkeyup='checkMany(this, "@Model.MaxPriceMyVSM" , "@Model.MaxPriceConferenceVSM" , "MaxPriceMyVSMOverflow" , "MaxPriceConferenceVSMOverflow" );' />&nbsp;<label style="display:inline;" id="lblPriceFree">$</label>
        </div>
        <div class="wz_s0_price_err_b">
            <div id="PriceRequired" class="paid_bad_summ" style="display:none;">@Html.GetGlobalResource("OnlineWizard", "PriceRequired")</div>
            <div id="NumbersAllowed" class="paid_bad_summ" style="display:none;">@Html.GetGlobalResource("OnlineWizard", "NumbersAllowed")</div>
            <div id="MaxPriceConferenceVSMOverflow" class="paid_bad_summ" style="display:none;">@Html.GetGlobalResource("OnlineWizard", "MaxPriceConferenceVSMOverflow")</div>
            <div id="MaxPriceMyVSMOverflow" class="paid_bad_summ" style="display:none;">@Html.GetGlobalResource("OnlineWizard", "MaxPriceMyVSMOverflow")</div>
        </div>
        <table class="wz_s0_free_par_t">
            <tr id="freeBlock" style="width:50%;">
                <td>
                    <label>@Html.GetGlobalResource("OnlineWizard", "VieversCountSelect")</label>
                    <select id="VieversCountSelect" name="VieversCountSelect">
                        @foreach (var item in Model.MaxViewerCntMult)
                        {
                            <option value="@item.Key">@item.Key</option>
                        }
                    </select>
                </td>
                <td>
                    <label>@Html.GetGlobalResource("OnlineWizard", "VideoContentMaxTime")</label>
                    <select id="MaxTimeMult" name="MaxTimeMult">
                        @foreach (var item in Model.MaxTimeMult)
                        {
                            <option value="@item.Key">@item.Key</option>
                        }
                    </select>
                </td>
            </tr>

                </table>
        </div>
        <div><span id="NeedManyText">@Html.GetGlobalResource("OnlineWizard", "NeedManyText")</span><span name="NeedManyValue" id="NeedManyValue"></span></div>
        <div class="SmallManyTextW" id="SmallManyTextW">@Html.GetGlobalResource("OnlineWizard", "SmallManyTextW")</div>
    </form>
